package com.welove.adapter;



import java.util.List;

import com.welove.R;
import com.welove.widget.SquareLayout;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

public class FriendAdapter extends BaseAdapter implements OnClickListener{
	private List<String> list;
	private Context context;

	public FriendAdapter(Context context) {
		this.context = context;
	}

	public void setData(List<String> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		// 每列两项
		if (list.size() % 3 == 0) {
			return list.size() / 3;
		}

		return list.size() / 3 + 1;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder viewHolder = null;
		if (convertView == null) {
			viewHolder = new ViewHolder();
			convertView = View.inflate(context, R.layout.friend_lv_item, null);
			viewHolder.friend_lv_item_iv1 = (ImageView) convertView
					.findViewById(R.id.friend_lv_item_iv1);
			viewHolder.friend_lv_item_tv1 = (TextView) convertView
					.findViewById(R.id.friend_lv_item_tv1);
			viewHolder.friend_lv_item_iv2 = (ImageView) convertView
					.findViewById(R.id.friend_lv_item_iv2);
			viewHolder.friend_lv_item_tv2 = (TextView) convertView
					.findViewById(R.id.friend_lv_item_tv2);
			viewHolder.friend_lv_item_iv3 = (ImageView) convertView
					.findViewById(R.id.friend_lv_item_iv3);
			viewHolder.friend_lv_item_tv3 = (TextView) convertView
					.findViewById(R.id.friend_lv_item_tv3);

			viewHolder.friend_lv_item_fm1=(FrameLayout)convertView
					.findViewById(R.id.friend_lv_item_fm1);
			viewHolder.friend_lv_item_fm2=(FrameLayout)convertView
					.findViewById(R.id.friend_lv_item_fm2);
			viewHolder.friend_lv_item_fm3=(FrameLayout)convertView
					.findViewById(R.id.friend_lv_item_fm3);
			convertView.setTag(viewHolder);

		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		
		viewHolder.friend_lv_item_fm1.setTag(position*3+1);
		viewHolder.friend_lv_item_fm2.setTag(position*3+2);
		viewHolder.friend_lv_item_fm3.setTag(position*3+3);

		viewHolder.friend_lv_item_fm1.setOnClickListener(this);
		viewHolder.friend_lv_item_fm2.setOnClickListener(this);
		viewHolder.friend_lv_item_fm3.setOnClickListener(this);
	
//		if ((position * 2 + 1) < list.size()) {
//			String string2 = list.get(position * 2 + 1);
//			viewHolder.iv2.setText(string2);
//			viewHolder.button2.setTag(string2);
//			viewHolder.button2.setOnClickListener(new OnClickListener() {
//
//				@Override
//				public void onClick(View v) {
//					// TODO Auto-generated method stub
//
//					Log.d("PAGE", v.getTag().toString());
//				}
//			});
//		} else {
//			//此处可以用一张透明图片替换 避免Grid布局混乱
//			viewHolder.button2.setVisibility(View.INVISIBLE);
//		}
		return convertView;
	}

	class ViewHolder {

		private FrameLayout friend_lv_item_fm1;
		private ImageView friend_lv_item_iv1;
		private TextView friend_lv_item_tv1;
		private FrameLayout friend_lv_item_fm2;
		private ImageView friend_lv_item_iv2;
		private TextView friend_lv_item_tv2;
		private FrameLayout friend_lv_item_fm3;
		private ImageView friend_lv_item_iv3;
		private TextView friend_lv_item_tv3;

	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		//进入详情页面
		//Intent todetail=new Intent();
		System.out.println("onClick---"+v.getTag().toString());
	}

}
